package com.jy.adscm.declarationForm.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.declarationForm.bean.JyDeclarationForm;
import com.jy.adscm.declarationForm.service.JyDeclarationFormService;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 经销商报单表 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2019-01-21
 */
@RestController
@RequestMapping("/adscm/dealer/jyDeclarationForm")
public class JyDeclarationFormController {


    @Autowired
    JyDeclarationFormService jyDeclarationFormService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;

    @Autowired
    JyCodeMapper jyCodeMapper;


    /**
     * 报单申请之前，先查询当前医院的当前科室的报单状态
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryApplyFormStatus")
    @AdscmLogger("报单申请之前，先查询当前医院的当前科室的报单状态")
    public SimpleCoreResponse queryApplyFormStatus(@RequestBody JyDeclarationForm jyDeclarationForm, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        JyUser user = AdscmBaseUtils.getInstance().getUser(tokenId, tokenRedisTemplate);
        if (user != null){
            Integer unitId = user.getUnitId();
            if (unitId != null && unitId >0){
                jyDeclarationForm.setApplyDealerId(unitId);

            }else {
                throw new AdscmSystemException("当前登录人员属于生产厂商，不能提交报单申请！");
            }
        }else {
            throw new AdscmSystemException("没有获取到当前登录人的登录信息，请重新登录");
        }

        return jyDeclarationFormService.queryApplyFormStatus(jyDeclarationForm);
    }

    /**
     * 经销商报单申请（增加报单记录）
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/addDeclarationForm")
    @AdscmLogger("经销商报单申请（增加报单记录）")
    public SimpleCoreResponse addDeclarationForm(@RequestBody JyDeclarationForm jyDeclarationForm, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        JyUser user = AdscmBaseUtils.getInstance().getUser(tokenId, tokenRedisTemplate);

        if (user != null){
            Integer unitId = user.getUnitId();
            if (unitId != null && unitId >0){

                jyDeclarationFormService.addDeclarationForm(user,jyDeclarationForm);
            }else {
                throw new AdscmSystemException("当前登录人员属于生产厂商，不能提交报单申请！");
            }
        }else {
            throw new AdscmSystemException("没有获取到当前登录人的登录信息，请重新登录");
        }


        return new SimpleCoreResponse("200","申请报单成功",null);
    }

    /**
     * 查询某个经销商的报单申请
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryApplyByDealerId/{dealerId}")
    @AdscmLogger("查询某个经销商的报单申请")
    public SimpleCoreResponse queryApplyByDealerId(@PathVariable Integer dealerId) throws AdscmSystemException {

        List<Integer> list = new ArrayList<>();
        list.add(dealerId);

        return new SimpleCoreResponse("200","查询报单申请成功",jyDeclarationFormService.queryApplyByDealerId(list,2));
    }
    /**
     * 查询当前登录用户所在经销商的报单申请，如果当前登录用户是厂商的用户，则查询不到
     * @param type ： 是否查询意向队列，如果是 (1) ，就只查询意向队列，如果是(0)，则查询：待审核、审核通过、审核不通过的，如果是（2）就只查审核通过的，  如果是其他的，则所有都查
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryApplyByLoginer/{type}")
    @AdscmLogger("查询当前登录用户所在经销商的报单申请")
    public SimpleCoreResponse queryApplyByLoginer(HttpServletRequest request,@PathVariable Integer type) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        JyUser user = AdscmBaseUtils.getInstance().getUser(tokenId, tokenRedisTemplate);

        List<JyDeclarationForm> formList = null;
        if (user != null){
            if (user.getUnitId() != null && user.getUnitId() != 0){

                List<Integer> list = new ArrayList<>();
                list.add(user.getUnitId());
                formList = jyDeclarationFormService.queryApplyByDealerId(list,type);
            }

        }
        if (formList == null){
            formList = new ArrayList<>();
        }
        return new SimpleCoreResponse("200","查询报单申请成功",formList);
    }

    /**
     * 经销商查询某个报单申请详情
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryDealerApplyFormDetail/{declarationFormId}")
    @AdscmLogger("经销商查询某个报单申请详情")
    public SimpleCoreResponse queryDealerApplyFormDetail(@PathVariable Long declarationFormId) throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询报单申请详情成功",jyDeclarationFormService.queryDealerApplyFormDetail(declarationFormId));
    }
    /**
     * 经销商撤销报单申请
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/revokeDealerApply/{declarationFormId}")
    @AdscmLogger("经销商撤销报单申请")
    public SimpleCoreResponse revokeDealerApply(@PathVariable Long declarationFormId) throws AdscmSystemException {

        JyDeclarationForm form = jyDeclarationFormService.selectById(declarationFormId);
        if (form != null){
            form.setApplyStatus(3);
            jyDeclarationFormService.updateById(form);
        }else {
            throw new AdscmSystemException("没有找打对应的报单信息，撤销失败！");
        }


        return new SimpleCoreResponse("200","撤销成功",null);
    }

    /**
     * 查询每个过程记录的期限
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getGcjlQx")
    @AdscmLogger("查询每个过程记录的期限")
    public SimpleCoreResponse getGcjlQx() throws AdscmSystemException {

        Wrapper<JyCode> wrapper = new EntityWrapper<>();
        wrapper.like("name","form_process_");
        List<JyCode> jyCodes = jyCodeMapper.selectList(wrapper);
        if (jyCodes == null){
            jyCodes = new ArrayList<>();
        }
        return new SimpleCoreResponse("200","查询成功",jyCodes);
    }
}

